<?php

namespace App\Controller\Wap;

use App\Base\BaseController4Restful;
use App\Entity\User;
use App\Repository\SchoolRepository;
use App\Repository\UserRepository;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/wap")
 */
class WapLoginController extends BaseController4Restful
{
    /**
     * @Route("/reg/page", name="WapAppRage",methods={"GET","POST"})
     */
    public function WapAppRage(
    )
    {
        return $this->render("wap/reg.html.twig");
    }
    /**
     * @Route("/reg/prepare", name="WapAppReg",methods={"GET","POST"})
     */
    public function WapAppReg(
    )
    {
        $data=[];
        $data["schools"] = [];
        return $this->json_out(1,"登陆成功",$data);
    }
    /**
     * @Route("/reg/do", name="WapAppRegDo",methods={"GET","POST"})
     */
    public function WapAppRegDo(
        UserRepository $userRepository
    )
    {
        $data=$this->getRequestData();
        if (!$name = $data["name"]??"") {
            return $this->json_out(0,"请输入姓名");
        }
        if (!$phone = $data["phone"]??"") {
            return $this->json_out(0,"请输入手机号");
        }
        $invite = $data["invite"]??"";
        $find = $userRepository->findOneBy(array("phone"=>$phone));
        if ($find) {
            return $this->json_out(0,"账号已存在请勿重复申请");
        }

        $user = new User();
        $user->setCreateAt(new \DateTime())
            ->setIsOn(false)
            ->setName($name)
            ->setPhone($phone)
            ->setInvitePhone($invite)
            ->setPassword($phone)
            ->setApiToken($phone)
            ->setExpireAt(new \DateTime());

        $this->myEm()->persist($user);
        $this->myEm()->flush();

        return $this->json_out(1,"信息已登记等待管理员审核",$data);
    }
}
